package org.study.itcast;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @author: jzhou
 * @date: 2024/4/20-22:42
 * @version: 1.0
 * @description: spark3.3-study
 */

@Slf4j
public class TestFileChannelTransferTo {
    public static void main(String[] args) {
        try (FileChannel inChannel = new FileInputStream("netty-demo/3parts.txt").getChannel();
             FileChannel outChannel = new FileOutputStream("netty-demo/3parts-to.txt").getChannel()
        ) {

            long size = inChannel.size();
            // Left 代表还剩余多少字节没有处理完
            for (long left = size; left > 0; ) {
                // transferTo 返回实际传输字节数
                long transferSize = inChannel.transferTo(size - left, left, outChannel);
                left -= transferSize;

            }

        } catch (IOException e) {
            log.error("error", e);
        }

    }
}
